<?php

require_once(sfConfig::get('sf_plugins_dir'). '/sfAssetsLibraryPlugin/modules/sfAsset/lib/BasesfAssetActions.class.php');

class sfAssetActions extends BasesfAssetActions
{

    public function executeAddAssetRowAjax(sfWebRequest $req){
        $this->forward404Unless($index = $req->getParameter('index') );
        $this->forward404Unless($widget_name = $req->getParameter('widget_name'));
        
        $widget = new sfInputAssets();
        //$widget->setAttribute('name',$widget_name);
        
        return $this->renderText( $widget->render($widget_name."[{$index}]") );
    }
    
    protected function updatesfAssetFromRequest(){

        parent::updatesfAssetFromRequest();

        if($this->getRequest()->hasFiles()){

            $folder   = $this->sf_asset->getFolderPath();
            $filename = $this->sf_asset->getFilename();
            $filename = substr($filename,0,-4).'.jpg';


            //$this->sf_asset->create($this->getRequest()->getFilePath('thumb_file'));

            $thumb_original_path  = sfAssetsLibraryTools::getThumbnailPath($folder, $filename, 'original');


            $this->getRequest()->moveFile('sf_asset[thumb_file]', $thumb_original_path);
            //echo "<pre>";print_r($this->getRequest()->getFiles());exit;
            $thumbnailSettings = sfConfig::get('app_sfAssetsLibrary_thumbnails', array(
            'small'  => array('width' => 84, 'height' => 84, 'shave' => true),
            'large'  => array('width' => 194, 'height' => 152)
            ));
            foreach ($thumbnailSettings as $key => $params)
            {
                $width  = $params['width'];
                $height = $params['height'];
                $shave  = isset($params['shave']) ? $params['shave'] : false;
                sfAssetsLibraryTools::createThumbnail($thumb_original_path
                , sfAssetsLibraryTools::getThumbnailPath($folder, $filename, $key), $width, $height, $shave);
            }

        }

    }

    public function executeAjax_get_thumbs(){
        $asset_id = $this->getRequestParameter('asset_id');
        //$thumbs = sfAssetThumbPeer::retrieveByPK($asset_id);
        $this->forward404Unless($asset_id);
        $t = is_callable(array('globals','_get_thumbs_dims'));
        
        $thumbs = $t ? globals::_get_thumbs_dims() : array();
        return $this->renderPartial('sfAsset/thumbs/form_thumbs',array('asset_id'=>$asset_id,'thumbs_templates'=>$thumbs));
    }

    public function executeCreate_thumb(sfWebRequest $req){
        $this->asset = sfAssetPeer::retrieveByPK($this->getRequestParameter('asset_id'));
        $this->w = $this->getRequestParameter('w');
        $this->h = $this->getRequestParameter('h');
        $this->no_width_height_resrection = $this->getRequestParameter('no_width_height_resrection');
        $tiny_mce = sfContext::getInstance()->getRequest('tinymce') ? '&tinymce=1' : '';

        if( ( !$this->w || !$this->h ) && !$this->no_width_height_resrection ){
            $this->redirect('sfAsset/Choose_thumb_template?asset_id='.$this->asset->getId().$tiny_mce);
        }
        

        

        if($req->isMethod('post'))  {

            if($this->no_width_height_resrection){
                $this->w = $this->w ? $this->w : $this->getRequestParameter('img_w') ;
                $this->h = $this->h ? $this->h : $this->getRequestParameter('img_h') ;
            }
            
            $pathes = $this->asset->getFilepaths();
            $full_path = !empty($pathes['original']) ?  $pathes['original'] : $pathes['full'];


            sfAssetToolkit::_create_cropped_image(
              $full_path
            , $this->asset->getId().'_'.$this->asset->getFilename()
            , $this->w
            , $this->h
            , $this->getRequestParameter('img_x')
            , $this->getRequestParameter('img_y')
            , $this->getRequestParameter('img_w')
            , $this->getRequestParameter('img_h')
            );
            $thumb = sfAssetThumbPeer::get_by_all($this->asset->getId(),$this->w,$this->h ,$create_thumb_image = false );
            if(!$thumb){
                $thumb = new sfAssetThumb();
            }
            $thumb->setsfAsset($this->asset);
            $thumb->setWidth($this->w);
            $thumb->setHeight($this->h);
            $thumb->save();
            $this->done = true;
        }

        //$thumbs = sfAssetThumbPeer::retrieveByPK($thumb_id);
        //$this->forward404Unless($asset_id);
    }


    public function executeChoose_thumb_template(sfWebRequest $req){
        $this->asset = sfAssetPeer::retrieveByPK($this->getRequestParameter('asset_id'));
        $this->thumbs_dim = sfAssetToolkit::_get_thumbs_dims();

        //echo "<pre>";print_r($this->thumbs_dim);exit;
    }
}

